﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace FastCtr
{
    public partial class NewLogHelper : UserControl
    {
        private DateTime dtInfo;
        private DateTime dtWarn;
        private Log_Helper log_helper;
        private string _logsPath = "Logs";
        private int _tsHour = 2;

        [Category("新增")]
        public string LogsPath
        {
            get
            {
                return _logsPath;
            }
            set
            {
                _logsPath = value;
            }
        }

        //时间间隔
        public int TSHour
        {
            get
            {
                return _tsHour;
            }
            set
            {
                _tsHour = value;
            }
        }

        public Log_Helper Log_helper
        {
            get
            {
                return log_helper;
            }
        }

        public NewLogHelper()
        {
            InitializeComponent();

            _logsPath = Application.StartupPath + "\\Logs";
            log_helper = new Log_Helper(_logsPath, _tsHour);

            log_helper.eventDispProcess -= new Log_Helper.delegateDispProcess(log_helper_eventDispProcess);
            log_helper.eventDispProcess += new Log_Helper.delegateDispProcess(log_helper_eventDispProcess); 
        }

        //写入TXT
        public void SaveTxt(string strName, string strData, bool bAppend)
        {
            log_helper.SaveTxt(strName, strData, bAppend);
        }

        //信息日志
        public void DispInfo(string data, MessageType messageType, bool bShow)
        {
            log_helper.DispInfo(data, messageType, bShow);
        }

        //异常日志
        public void DispWarn(string data, MessageType messageType, bool bShow)
        {
            log_helper.DispWarn(data, messageType, bShow);
        }

        private void log_helper_eventDispProcess(string msg, Color foreColor)
        {
            if (this.Parent != null)
            {
                this.BeginInvoke((MethodInvoker)delegate ()
                {
                    updataRichText(txtProcessDisp, 20);
                    txtProcessDisp.Focus();
                    txtProcessDisp.SelectionColor = foreColor;
                    txtProcessDisp.AppendText(msg);

                    //滚动条到最下面
                    txtProcessDisp.Select(txtProcessDisp.Text.Length, 0);
                    txtProcessDisp.ScrollToCaret();
                });
            }
        }

        /// <summary>
        /// 限制richTextbox的行数
        /// </summary>
        /// <param name="richTextBox"></param>
        /// <param name="maxLinds"></param>
        private void updataRichText(System.Windows.Forms.RichTextBox richTextBox, int maxLinds)
        {
            if (richTextBox.Lines.Length >= maxLinds)
            {
                richTextBox.SelectionStart = 0;
                richTextBox.SelectionLength = richTextBox.Text.IndexOf("\n") + 1;
                richTextBox.ReadOnly = false;
                richTextBox.SelectedText = "";
                richTextBox.ReadOnly = true;

                //光标聚焦在最后一行
                richTextBox.Select(richTextBox.Text.Length, 0);
                richTextBox.ScrollToCaret();
            }
        }

        private void label1_DoubleClick(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {
            try
            {
                string strPath = "";
                strPath = _logsPath;
                strPath += "\\";
                strPath += DateTime.Now.ToString("yyyyMMdd");
                strPath += "\\";
                string[] arrFiles = Directory.GetFiles(strPath, "Logs_*");
                if (arrFiles.Length == 0)
                {
                    MessageBox.Show("日志文件为空！");
                    return;
                }

                foreach (var file in arrFiles)
                {
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.LastWriteTime >= dtInfo)
                    {
                        dtInfo = fileInfo.LastWriteTime;
                        strPath = file;
                    }
                }
                Process.Start(strPath);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {
            try
            {
                string strPath = "";
                strPath = _logsPath;
                strPath += "\\";
                strPath += DateTime.Now.ToString("yyyyMMdd");
                strPath += "\\";
                string[] arrFiles = Directory.GetFiles(strPath, "Warns_*");
                if (arrFiles.Length == 0)
                {
                    MessageBox.Show("报警日志文件为空！");
                    return;
                }

                foreach (var file in arrFiles)
                {
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.LastWriteTime >= dtWarn)
                    {
                        dtWarn = fileInfo.LastWriteTime;
                        strPath = file;
                    }
                }
                Process.Start(strPath);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
